package procilege.impl;

import java.util.logging.Logger;

import javax.xml.namespace.QName;

import merisis.contentcloud.ContentCloud;
import merisis.sense.SenseMaker;
import merisis.traitspace.EntityHappen;
import merisis.traitspace.HappenReaction;
import procilege.lang.PrivExprElement;

import com.google.inject.Inject;

public class PrivilegeFormReaction implements HappenReaction {

	@Inject
	private Logger logger;

	@Inject
	private EntityHappen eh;

	@Inject
	private ContentCloud cloud;

	@Inject
	private SenseMaker smr;

	public void happen(long[] points) {

		long metaNormalForm = smr.sense(new QName(
				ComputationalEntityMgr.PAL_METAENTITY_NS,
				ComputationalEntityMgr.PAL_PNORMAL_EXPR));

		for (int i = 0; i < points.length; i++) {

			long origplace = points[i];
			Object content = cloud.retrieve(origplace);
			if (content == null || !(content instanceof PrivExprElement)) {
				logger.warning(String.format("illegal content: %s", content));
				continue;
			}

			PrivilegeFormImpl pf = new PrivilegeFormImpl(new NameImpl(origplace));
			pf.isDirty = true;
			pf.expr = (PrivExprElement) content;

			eh.happen(new long[] { metaNormalForm }, pf, origplace);
		}
	}
}
